package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.home.fragment.CheckLogDetailFragment;
import com.example.safeassistan.home.fragment.PersonCommentFragment;
import java.util.ArrayList;
import java.util.List;

public class CheckLogSecond  extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private ViewPager viewPager;
    private List<String> mTitleList; //Tab的标题
    private List<Fragment> fragmentList;
    private MainAdapter adapter;
    private TextView tvLogDetail,tvPersonComment;
    private String recordTableId="";
    private String checkTableId="";
    private String riskPointId="";
    private String defId="";

    public static void actionStart(Context context,String recordTableId,String checkTableId,String defId,String riskPointId) {
        Intent intent = new Intent(context, CheckLogSecond.class);
        intent.putExtra("recordTableId",recordTableId);
        intent.putExtra("checkTableId",checkTableId);
        intent.putExtra("defId",defId);
        intent.putExtra("riskPointId",riskPointId);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.tvLogDetail:
                viewPager.setCurrentItem(0);
                break;
            case R.id.tvPersonComment:
                viewPager.setCurrentItem(1);
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.checklog_second;
    }

    @Override
    protected void initView() {
        recordTableId=getIntent().getStringExtra("recordTableId");
        checkTableId=getIntent().getStringExtra("checkTableId");
        riskPointId=getIntent().getStringExtra("riskPointId");
        defId=getIntent().getStringExtra("defId");
        ivBack=findViewById(R.id.ivBack);
        ivBack.setOnClickListener(this::onClick);
        viewPager=findViewById(R.id.viewPager);
        tvPersonComment=findViewById(R.id.tvPersonComment);
        tvPersonComment.setOnClickListener(this::onClick);
        tvLogDetail=findViewById(R.id.tvLogDetail);
        tvLogDetail.setOnClickListener(this::onClick);

        mTitleList=new ArrayList<>();
        mTitleList.add("记录详情");
        mTitleList.add("人员评价");
        fragmentList=new ArrayList<>();
        fragmentList.add(CheckLogDetailFragment.newInstance(recordTableId));
        fragmentList.add(PersonCommentFragment.newInstance(1,checkTableId,defId,riskPointId));
        adapter=new MainAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                switch (position){
                    case 0:
                        reSetStatus();
                        tvLogDetail.setTextColor(getResources().getColor(R.color.white));
                        tvLogDetail.setBackground(getResources().getDrawable(R.drawable.leftcorner_blue_bg_5));
                        break;
                    case 1:
                        reSetStatus();
                        tvPersonComment.setTextColor(getResources().getColor(R.color.white));
                        tvPersonComment.setBackground(getResources().getDrawable(R.drawable.rightcorner_blue_bg));
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) { }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }

    public void reSetStatus(){
        tvLogDetail.setBackground(null);
        tvPersonComment.setBackground(null);
        tvPersonComment.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
        tvLogDetail.setTextColor(getResources().getColor(R.color.blue_3B5BEA));
    }

    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mTitleList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_NULL;
    }
}